package chances.epg.navigator.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;

import chances.epg.commons.utils.PathUtils;
import chances.epg.commons.utils.RequestUtils;
import chances.epg.commons.utils.SpringWebUtils;
import chances.epg.config.BaseMvcConfig;
import chances.epg.entity.biz.Biz;
import chances.epg.navigator.EpgConstants;
import chances.epg.navigator.context.AccessContext;
import chances.epg.navigator.history.HistoryEntry;
import chances.epg.navigator.history.NavigateHistory;
import chances.epg.navigator.history.NavigateHistoryFactory;
import chances.epg.navigator.service.NavigateUrlFactory;
import chances.epg.session.UserSession;
import chances.epg.session.UserSessionHelper;

@SuppressWarnings("serial")
public class HistoryParentTag extends TagSupport {

	private String var;

	private String code;

	private AccessContext _context;
	
	private boolean isNeedhost;

	@Override
	public int doEndTag() throws JspException {
		if (_context == null) {
			_context = (AccessContext) pageContext.findAttribute(EpgConstants.EPG_CONTEXT);
		}
		if (_context == null) {
			return SKIP_BODY;
		}
		UserSession userSession = UserSessionHelper.getUserSession(pageContext);
		if (userSession == null) {
			return SKIP_BODY;
		}

		HttpServletRequest httpRequest = (HttpServletRequest) pageContext.getRequest();
		NavigateHistoryFactory navigateHistoryFactory = SpringWebUtils.findBean(httpRequest, NavigateHistoryFactory.class);
		NavigateHistory history = navigateHistoryFactory.getHistory(httpRequest,userSession);
		NavigateUrlFactory navigateUrlFactory = SpringWebUtils.findBean(httpRequest, NavigateUrlFactory.class);
		
		Biz biz = _context.getBiz();
		String parentCode = _context.getBizCode();
		if(StringUtils.isNotEmpty(code)){
			parentCode = code;
		}
		if(!this.isDetail(httpRequest)){
			parentCode =  biz.getParentCode();
		}
		
		String path =  navigateUrlFactory.generateBizURL(_context, parentCode);
		setVar(userSession, history, path);
		this.code = null;
		return super.doEndTag();
	}

	private void setVar(UserSession userSession, NavigateHistory history, String path) {
		int idx = history.indexOf(path);
		if (idx == -1) {
			pageContext.setAttribute(var, userSession.getReturnUrl());
		} else {
			HistoryEntry entry = history.get(idx);
			if (entry != null) {
				String url = entry.getURL();
				url = checkUrl(path, url);
				pageContext.setAttribute(var, url);
			}
		}
	}

	private String checkUrl(String path, String url) {
		if (url.indexOf("/") == 0 && this.isNeedhost) {
			HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
			BaseMvcConfig baseMvcConfig = SpringWebUtils.findBean(pageContext, BaseMvcConfig.class);
			String host = baseMvcConfig.getEpgHost();
			url = PathUtils.join(host, path.replace(request.getContextPath(), ""));
		}
		return url;
	}
	
	public boolean isDetail(HttpServletRequest request){
		String path = RequestUtils.getRequestPath(request);
		if(path.contains("/item/") || path.contains("/det/")){
			return true;
		}else{
			return false;
		}
	}

	public void setVar(String var) {
		this.var = var;
	}

	public void setAccessContext(Object obj) {
		if (AccessContext.class.isInstance(obj)) {
			this._context = (AccessContext) obj;
		}
	}


	public void setCode(String code) {
		this.code = code;
	}

	public boolean isNeedhost() {
		return isNeedhost;
	}

	public void setIsNeedhost(boolean isNeedhost) {
		this.isNeedhost = isNeedhost;
	}

}
